/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoadingProgressBar.java
 *
 * Created on 2011/12/31, 上午 10:25:43
 */
package elaborate.task.ui;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lendle
 */
public class LoadingProgressBar extends javax.swing.JPanel {
    private EffectThread thread=null;
    /** Creates new form LoadingProgressBar */
    public LoadingProgressBar() {
        initComponents();
    }

    public synchronized void start(){
        if(thread!=null){
            return;
        }
        thread=new EffectThread();
        thread.start();
    }
    
    public synchronized void stop(){
        if(this.thread==null){
            return;
        }
        this.thread.shutdown();
        thread=null;
        this.jProgressBar1.setValue(0);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();

        setLayout(new java.awt.BorderLayout());
        add(jProgressBar1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar jProgressBar1;
    // End of variables declaration//GEN-END:variables

    class EffectThread extends Thread{

        protected boolean running=true;

        public void shutdown(){
            this.running=false;
        }

        public EffectThread() {
            this.setDaemon(true);
        }
        
        public void run(){
            while(running){
                if(LoadingProgressBar.this.jProgressBar1.getValue()<LoadingProgressBar.this.jProgressBar1.getMaximum()){
                    LoadingProgressBar.this.jProgressBar1.setValue(LoadingProgressBar.this.jProgressBar1.getValue()+10);
                }
                else{
                    LoadingProgressBar.this.jProgressBar1.setValue(0);
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(LoadingProgressBar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
